package com.gc.cscott.cli;

import com.gc.cscott.domain.Solution;
import com.gc.cscott.fs.FileSystemWorker;
import com.gc.cscott.domain.Competitor;
import com.google.common.collect.ImmutableList;
import com.google.inject.Inject;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

/**
 *
 * @author root
 */
public class ValidateSolutionsCommand implements Command {
    @Inject
    FileSystemWorker fsWorker;

    boolean modeRemoveEmptySolutionDirectories = false;

    public List<String> listHelp() {
        return Arrays.asList(
                "Prints all missconfigures solutions directores.",
                "Optional:",
                "  'remove_empty_z_directories' - remove empty solution directories");
    }

    public List<String> perform(List<String> args) {
        LinkedList<String> mutableArgs = new LinkedList<String>(args);
        if (mutableArgs.isEmpty()==false){
            if ("remove_empty_z_directories".equals(mutableArgs.getFirst())==true){
                modeRemoveEmptySolutionDirectories = true;
                mutableArgs.removeFirst();
            }
        }

        System.out.println("Validatig solutions:");
        List<Competitor> competitorsList = fsWorker.listCompetitorsAll();
        final int longestName = ReportUtils.longestName(competitorsList);
        for (Competitor competitor : competitorsList) {
            try {
                List<String> problems = new ArrayList<String>();
                for (File dir : fsWorker.listCompetitorsDirs(competitor)) {
                    if (dir.equals(competitor.getBasedir())) {
                        //ignore.
                    } else if (dir.getParentFile().equals(competitor.getBasedir()) == false) {
                        problems.add("Unknown subdirectory: " + dir.getAbsolutePath());
                    } else if (fsWorker.getTaskNumberBySolutionDirectory(dir) == null) {
                        problems.add("Wrong name: " + dir.getAbsolutePath());
                    } else {
                        try {
                            fsWorker.findExecutableInSolution(new Solution(dir, fsWorker.getTaskNumberBySolutionDirectory(dir)));
                        } catch (FileNotFoundException noSolution) {
                            problems.add("No executables: " + dir.getAbsolutePath());
                            File[] nestedFiles = dir.listFiles();
                            for (File file : nestedFiles) {
                                problems.add("  but there is a "
                                        + (file.isFile() ? "file" : "subdirectory")
                                        + ": " + file.getName());
                            }
                            if (nestedFiles.length == 0) {
                                problems.add("  <no files in directory>");
                                if (modeRemoveEmptySolutionDirectories){
                                    if (dir.delete()==false){
                                        problems.add("  FAILURE: Can not delete empty directory: "+dir.getAbsolutePath());
                                    } else {
                                        problems.add("  removed.");
                                    }
                                }
                            }
                        }
                    }
                }
                System.out.printf("  %-" + longestName + "s - %s", competitor.getName(),
                        problems.isEmpty() ? "OK" : "WANRING");
                System.out.println();
                for (String problem : problems) {
                    System.out.println("    " + problem);
                }
            } catch (IOException ex) {
                throw new Error(ex);
            }
        }
        return ImmutableList.copyOf(mutableArgs);
    }
}
